import {ScreenSpaceEventHandler,ScreenSpaceEventType,Cartesian2} from "cesium"
import "./mapinfo.css"
const mapInfo = function(viewer){
    var self = this
    this.coordinatesDiv = document.createElement("div");
    // this.coordinatesDiv.id = this.guids.coordinatesId
    this.coordinatesDiv.className = "map3D-coordinates";
    this.coordinatesDiv.innerHTML = "<span id='cd_label' style='font-size:13px;text-align:center;font-family:微软雅黑;color:#edffff;'>暂无坐标信息</span>";
    // var body = document.getElementsByTagName("body")[0];
    // body.appendChild(dom);
    var ViewDom = viewer.container
    ViewDom.appendChild(this.coordinatesDiv);
    if(viewer){
        var handler3D = new ScreenSpaceEventHandler(viewer.scene.canvas);
        handler3D.setInputAction(function(movement) {
            var pick= new Cartesian2(movement.endPosition.x,movement.endPosition.y);
            if(pick){
                var cartesian = viewer.scene.globe.pick(viewer.camera.getPickRay(pick), viewer.scene);
                if(cartesian){
                    //世界坐标转地理坐标（弧度）
                    var cartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(cartesian);
                    if(cartographic){
                        //海拔
                        var height = viewer.scene.globe.getHeight(cartographic);
                        //视角海拔高度
                        var he = Math.sqrt(viewer.scene.camera.positionWC.x * viewer.scene.camera.positionWC.x + viewer.scene.camera.positionWC.y * viewer.scene.camera.positionWC.y + viewer.scene.camera.positionWC.z * viewer.scene.camera.positionWC.z);
                        var he2 = Math.sqrt(cartesian.x * cartesian.x + cartesian.y * cartesian.y + cartesian.z * cartesian.z);
                        //地理坐标（弧度）转经纬度坐标
                        var point=[ cartographic.longitude / Math.PI * 180, cartographic.latitude / Math.PI * 180];
                        if(!height){
                            height = 0;
                        }
                        if(!he){
                            he = 0;
                        }
                        if(!he2){
                            he2 = 0;
                        }
                        if(!point){
                            point = [0,0];
                        }
                        self.coordinatesDiv.innerHTML = "<span id='cd_label' style='font-size:13px;text-align:center;font-family:微软雅黑;color:#edffff;'>"+"视角海拔高度:"+(he - he2).toFixed(2)+"米"+"&nbsp;&nbsp;&nbsp;&nbsp;海拔:"+height.toFixed(2)+"米"+"&nbsp;&nbsp;&nbsp;&nbsp;经度：" + point[0].toFixed(6) + "&nbsp;&nbsp;纬度：" + point[1].toFixed(6)+ "</span>";
                    }
                }
            }
        }, ScreenSpaceEventType.MOUSE_MOVE);
    }
}
export default mapInfo